﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Collections;
using System.Xml.Linq;

namespace Sofire.Data.XSql
{
    internal class XSqlTrimNode : XSqlTrimNodeBase, IXSqlTrimNode
    {
        public const string NodeName = "trim";
        public override XSqlType Type
        {
            get { return XSqlType.Trim; }
        }

        private string _Prefix;
        public string Prefix
        {
            get
            {
                return this._Prefix;
            }
            set
            {
                this._Prefix = value;
            }
        }

        public override XObject CreateXObject()
        {
            XElement element = new XElement(NodeName);
            element.SetAttributeValue(XSqlAttributes.Prefix, this._Prefix);
            element.SetAttributeValue(XSqlAttributes.Trim, this.Trim);
            this.CreateXChildren(element);
            return element;
        }

        protected override void Load(XmlNode node)
        {
            base.Load(node);
            this._Prefix = this.TryGetAttribute(node, XSqlAttributes.Prefix);
        }

        public override string Compile(XSqlParameterCollection parameters)
        {
            var text = this.Children.Compile(parameters);
            if(text == null) return null;
            text = this.TrimText(text);
            return this._Prefix + " " + text;
        }
    }
}
